export default {
  data() {
    return {
      isPlay: false,
      audio: new Audio()
    }
  },
  methods: {
    /**
     * 播放音频
     */
    play(src) {
      if (!this.isPlay) {
        this.audio.src = src
        setTimeout(() => {
          this.audio.play()
          this.isPlay = true
          this.audio.addEventListener(
            'ended',
            () => {
              this.pause()
            },
            false
          )
        }, 10)
      } else {
        this.pause()
      }
    },
    pause() {
      this.audio.pause()
      this.isPlay = false
      this.audio.removeEventListener(
        'ended',
        () => {
          alert('over')
        },
        false
      )
    }
  }
}
